<?php

namespace app\ruinuo\controller;

use app\ruinuo\config\database\Lists as ThatLists;
use app\ruinuo\config\database\Logic as ThatLogic;
use app\common\service\DataBase as DataBaseService;
use think\facade\Config;
use think\facade\Db;
use think\facade\Request;
use app\common\middleware\Auth;
use app\common\middleware\Power;
use app\common\middleware\Log;

class Database
{
    protected $middleware = [Auth::class => [], Power::class => [], Log::class => []];

    /*列表模板*/
    public function Index()
    {
        return ViewTpl('ruinuo/database/lists');
    }

    /*列表模板及数据*/
    public function Lists()
    {
        if (Request::isAjax() || Request::isPost()) {
            $ThatLists = new ThatLists();
            $data['rows'] = $ThatLists->Lists(input());
            $data['total'] = $ThatLists->Count(input());
            return BackCode($data);
        }
    }

    /*列表配置*/
    public function Tabs()
    {
        if (Request::isAjax() || Request::isPost()) {
            $ThatLists = new ThatLists();
            $data = $ThatLists->Tabs(input());
            return BackCode($data);
        }
    }

    /*明细*/
    public function Detail()
    {
        if (Request::isAjax() || Request::isPost()) {
            $ThatLogic = new ThatLogic();
            $data = $ThatLogic->Detail(input());
            $field = !empty($data['data']['detail']['field']) ? json_decode($data['data']['detail']['field'], true) : [];
            foreach ($field as $k => $v) {
                $field[$k]['old_field'] = $v['field'];
                if ($v['field'] == 'id' || $v['field'] == 'create_time' || $v['field'] == 'create_id' || $v['field'] == 'update_time' || $v['field'] == 'update_id') {
                    unset($field[$k]);
                }
            }
            $data['data']['detail']['old_name'] = $data['data']['detail']['name'];
            $data['data']['detail']['field'] = json_encode(array_values($field), JSON_UNESCAPED_UNICODE);
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
        return ViewTpl('ruinuo/database/form');
    }

    /*新增数据*/
    public function Add()
    {
        if (Request::isAjax() || Request::isPost()) {
            $ThatLogic = new ThatLogic();
            $params = $this->TableField(input());
            $table = Config::get('database.connections.mysql.prefix') . $params['name'];
            $sql = "SHOW TABLES LIKE '" . $table . "'";
            if (count(Db::query($sql)) > 0) {
                return BackCode([], 203, '数据库重复');
            }
            $data = $ThatLogic->Add($params);
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
        return ViewTpl('ruinuo/database/form');
    }

    /*修改数据*/
    public function Edit()
    {
        if (Request::isAjax() || Request::isPost()) {
            $ThatLogic = new ThatLogic();
            $params = $this->TableField(input());
            $data = $ThatLogic->Edit($params);
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
        return ViewTpl('ruinuo/database/form');
    }

    public function TableField($params = [])
    {
        $field = !empty($params['field']) ? json_decode($params['field'], true) : [];
        $field[] = ['field' => 'id', 'title' => '主键', 'type' => 'int', 'length' => 11, 'key' => 1];
        $field[] = ['field' => 'create_time', 'title' => '新增时间', 'type' => 'datetime', 'length' => 0];
        $field[] = ['field' => 'update_time', 'title' => '修改时间', 'type' => 'datetime', 'length' => 0];
        $field[] = ['field' => 'create_id', 'title' => '新增人', 'type' => 'int', 'length' => 11, 'default' => 0];
        $field[] = ['field' => 'update_id', 'title' => '修改人', 'type' => 'int', 'length' => 11, 'default' => 0];
        $params['field'] = json_encode($field, JSON_UNESCAPED_UNICODE);
        return $params;
    }


    /*删除数据*/
    public function Delete()
    {
        if (Request::isAjax() || Request::isPost()) {
            $ThatLogic = new ThatLogic();
            $data = $ThatLogic->Delete(input());
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
    }

    /*批量删除数据*/
    public function AllDelete()
    {
        if (Request::isAjax() || Request::isPost()) {
            $ThatLogic = new ThatLogic();
            $data = $ThatLogic->Delete(input());
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
    }

    /*单个字段修改*/
    public function UpField()
    {
        if (Request::isAjax() || Request::isPost()) {
            $ThatLogic = new ThatLogic();
            $data = $ThatLogic->UpdateField(input());
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
    }

    /*导入Excel数据*/
    public function InExcel()
    {
        if (Request::isAjax() || Request::isPost()) {
            $file = Request::file("file") ?: Request::file("edit");
            $ThatLists = new ThatLists();
            $data = $ThatLists->InExcel($file);
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
    }

    /*导出Excel数据*/
    public function OutExcel()
    {
        $ThatLists = new ThatLists();
        $ThatLists->OutExcel(input());
    }

    /*下载Excel模板*/
    public function TemplateExcel()
    {
        $ThatLists = new ThatLists();
        $ThatLists->TemplateExcel();
    }


    public function Sync()
    {
        if (Request::isAjax() || Request::isPost()) {
            $DataBaseService = new DataBaseService();
            $DataBaseService->Sync(input("table_name"));
            return BackCode([], 200, '同步成功');
        }
    }


}